package org.erp.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.erp.common.annotation.StockLockBatch;
import org.erp.common.page.PageQueryModel;
import org.erp.product.domain.dto.ConnectToInnerAddDto;
import org.erp.product.domain.dto.TransferLogQueryDto;
import org.erp.product.domain.entity.ProductInfo;
import org.erp.product.domain.vo.ProductInfoVo;
import org.erp.product.infrastructure.convert.ConnectConvert;
import org.erp.product.infrastructure.convert.RepairTransferConvert;
import org.erp.product.infrastructure.enums.RepairEventEnum;
import org.erp.product.infrastructure.mapper.ConnectLogMapper;
import org.erp.product.infrastructure.mapper.ProductInfoMapper;
import org.erp.product.service.ProductInfoService;
import org.erp.product.service.RepairTransferLogService;
import org.erp.product.service.TransferLogService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author libin
 * @version 1.0
 * @description:
 * @date 2025/11/18 15:31
 **/
@Log4j2
@Service
@AllArgsConstructor
public class RepairTransferLogServiceImpl implements RepairTransferLogService {

    private final RepairTransferConvert repairTransferConvert;

    private final ConnectConvert connectConvert;

    private final ProductInfoService productInfoService;

    private final ProductInfoMapper productInfoMapper;

    private final TransferLogService transferLogService;

    private final ConnectLogMapper connectLogMapper;

    private final RabbitTemplate rabbitTemplate;

    @Override
    public PageQueryModel<ProductInfoVo> pageList(TransferLogQueryDto transferLogQueryDto) {
        return productInfoService.pageList(repairTransferConvert.dtoConvertDto(transferLogQueryDto));
    }

    @StockLockBatch(listField = "stockIds")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void transfer(ConnectToInnerAddDto connectToInnerAddDto) {
        List<Long> stockIds = connectToInnerAddDto.getStockIds();
        List<ProductInfo> list = searchAndVerify(stockIds, RepairEventEnum.TRANSFER);
//        Integer oldStatus = list.get(0).getRepairStatus();
//        list.forEach(w -> {
//            RepairTransferStatusEnum nextStatus = RepairStateFlow.next(RepairTransferStatusEnum.getByCode(w.getRepairStatus()), RepairEventEnum.TRANSFER);
//            w.setRepairStatus(nextStatus.getCode());
//        });
//        Integer newStatus = list.get(0).getRepairStatus();
//        //改状态
//        productInfoMapper.updateRepairBatchById(list);
//        // 加操作记录
//        List<TransferLog> transferLogList = TransferLog.getTransferLogList(list, oldStatus, newStatus);
//        transferLogService.saveBatch(transferLogList);
//        //创建接驳数据
//        List<ConnectLog> connectLogList = list.stream()
//                .map(c -> connectConvert.dtoConvert(connectToInnerAddDto, c)).toList();
//        connectLogList.forEach(c -> c.setSourceType(ConnectSourceTypeEnum.REPAIR.getCode()));
//        connectLogMapper.updateBatchById(connectLogList);
    }

    @StockLockBatch(directIds = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void returnBack(List<Long> stockIds) {
        List<ProductInfo> list = searchAndVerify(stockIds, RepairEventEnum.RETURN_BACK);
//        Integer oldStatus = list.get(0).getRepairStatus();
//        list.forEach(w -> {
//            RepairTransferStatusEnum nextStatus = RepairStateFlow.next(RepairTransferStatusEnum.getByCode(w.getRepairStatus()), RepairEventEnum.RETURN_BACK);
//            w.setRepairStatus(nextStatus.getCode());
//        });
//        Integer newStatus = list.get(0).getRepairStatus();
//        productInfoMapper.updateRepairBatchById(list);
//        //加操作记录
//        List<TransferLog> transferLogList = TransferLog.getTransferLogList(list, oldStatus, newStatus);
//        transferLogService.saveBatch(transferLogList);
//        // 发起异步消息
//        AsyncMessageStatusDto asyncMessageStatusDto = AsyncMessageStatusDto.buildAsyncMessageStatusDto(list);
//        rabbitTemplate.convertAndSend(RabbitmqConstant.ERP_STORE_EXCHANGE
//                , RabbitmqConstant.ERP_STORE_ROUTING_KEY, JSONObject.toJSONString(asyncMessageStatusDto));
    }

    public List<ProductInfo> searchAndVerify(List<Long> stockIds, RepairEventEnum repairTransferEventEnum) {
        List<ProductInfo> list = productInfoMapper.selectList(new QueryWrapper<ProductInfo>().lambda()
                .in(ProductInfo::getStockId,stockIds));
//        for (ProductInfo productInfo : list) {
//            try {
//                RepairStateFlow.next(RepairTransferStatusEnum.getByCode(productInfo.getRepairStatus()), repairTransferEventEnum);
//            } catch (Exception e) {
//                throw new ProductException("维修调拨id为: " + productInfo.getId() + " 状态不允许执行该操作");
//            }
//        }
        return list;
    }
}
